package com.finalministry.server;

import java.io.File;
import java.io.IOException;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.finalministry.server.action.ExportasKMLfileAction;

@Path("/export")
public class ServiceExport {

	@Path("/kml")
	@GET
	@Produces(MediaType.TEXT_HTML)
	public String export_KML(@QueryParam("kmlFile") String kmlFile) throws IOException {

		if (kmlFile == null || kmlFile.trim().length() == 0) {
			return "<h2><span class=\"label label-danger\">No filename specified!</span></h2><script>$('#kmlFile').focus();</script>";
		}

		if (kmlFile.indexOf("kml") == -1) {
			kmlFile = kmlFile + ".kml";
		}

		File localDirectory = new File("");
		String path = localDirectory.getAbsolutePath();
		File file = new File(path + "/export/" + kmlFile);

		ExportasKMLfileAction exportasKMLfileAction = new ExportasKMLfileAction();
		exportasKMLfileAction.exportTerritoriesAsKML(file);

		return "<h3><span class=\"label label-success\">Export successfull</span></h3><p>You can find your download here:<br/><pre>"
				+ file.getAbsolutePath() + "</pre></p>";
	}
}